require 'sinatra/base'
require 'data_mapper'
require 'date'

# Controllers/Handlers
## Web handlers
require './handlers/web'
require './handlers/errors'

## API-specific handlers
require './handlers/login'
require './handlers/users'
require './handlers/items'

# Helpers
require './helpers/logger'

# Database setup
## Require all model definitions
require './models/user'
require './models/item'
require './models/tag'

## Initialize the database
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/dev.db")
DataMapper.finalize
DataMapper.auto_upgrade!

class WheresMyStuffApi < Sinatra::Application
    use Rack::Session::Cookie, :secret => ENV['SESSION_SECRET'] || "afjkl2j4tQG@FED@Tg24fdjpG@42"
    use Rack::Logger

    ## Load various handlers. Each piece of functionality (items, users, login)
    ## has its own handler to make the code easier to maintain and alter
    use WebHandler
    use ErrorHandler
    use LoginHandler
    use ItemHandler
    use UserHandler

    ## Configuration
    configure do
        enable :logging
    end

    if app_file == $0
        $stdout.sync = true
        run!
    end
end
